<?php

/**
 * Audit models
 *
 * Audit models in bundles
 *
 * PHP Version: 1.2.3
 *
 * @author Farina <noor.erfia@3fresources.com>
 */

namespace Trail\Models;

use \Laravel\Database\Eloquent\Model as Eloquent;
use Datagrid as Datagrid;
use Laravel\Str;
use Laravel\Lang;
use Laravel\Config;

/**
 * Class Audit
 *
 * Class Audit in trail models bundles
 *
 * @author Farina <noor.erfia@3fresources.com>
 */

class Audit extends Eloquent
{

    public static $timestamps = true;
    public static $table = 'sys_audit_trail';
    public static $key = 'sat_id';

    /**
     * Function add audit trail that pass parameter into table sys_audit_trail
     *
     * @param string  $category the string to quote
     * @param int     $user     the integer to quote
     * @param varchar $logs     the varchar to quote
	 *
	 * @return parameter.
     */	
	public static function logTrail($category = 'General', $user = null, $logs = '')
	{
		$trail = new Audit;
		
		$trail->su_id = $user;
		$trail->sat_category = $category;	
		$trail->sat_remark = $logs;
		$trail->sat_tarikh = date("Y-m-d H:i:s");
		$trail->timestamp();
		$trail->save();
	}
	
	/**
     * Function view list audit trail
	 *
	 * @return list.
     */	
	public static function listTrail()
	{
		$allTrail = Audit::left_join('sys_user_profile', 'sys_audit_trail.su_id', '=', 'sys_user_profile.su_id')
					->order_by('sat_tarikh', 'desc')
                    ->paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;
		$datagrid->setFields(array('sat_category' => Str::upper(Lang::line('admin.cataudit')->get())));
		$datagrid->setFields(array('sat_remark' => Str::upper(Lang::line('admin.auditmark')->get())));
        $datagrid->setFields(array('sup_nama' => Str::upper(Lang::line('admin.suid')->get())));
        $datagrid->setDate(array('sat_tarikh' => Str::upper(Lang::line('admin.tkhaudit')->get())));
        $datagrid->setTable('sys_audit_trail', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->build($allTrail, 'sat_id');

        return $datagrid->render();
	}
	
    /**
     * Function search username audit trail
	 *
	 * @param varchar $input the varchar to quote
	 *
	 * @return list search username.
     */
	public static function trailSearchList($input)
	{
		$searchval = isset($input['searchval'])?$input['searchval']:false;
		$searchdat = isset($input['searchdat'])?$input['searchdat']:false;

        $allTrail = Audit::left_join('sys_user_profile', 'sys_audit_trail.su_id', '=', 'sys_user_profile.su_id')
                    ->where(function($query)use($searchval,$searchdat)
					    {
					    	// echo $searchdat
					    	if($searchval):
					        $query->where('sys_user_profile.sup_nama', 'LIKE', '%'.$searchval.'%');
					    	endif;
					    	if($searchdat):
					        $query->raw_where("DATE_FORMAT(sys_audit_trail.sat_tarikh,'%e-%m-%Y')='".$searchdat."'");
					    	endif;
					    })
					->order_by('sat_tarikh', 'desc')
                    ->paginate(Config::get('system.pagination'));

        if($searchval && $searchdat):
        	$appender = array('searchval' => $input['searchval'],'searchdat' => $input['searchdat']);
        elseif(!$searchval && $searchdat):
        	$appender = array('searchdat' => $input['searchdat']);
        elseif($searchval && !$searchdat):
        	$appender = array('searchval' => $input['searchval']);
        else:
        	$appender = array();
        endif;	

        $allTrail->appends($appender)->links();

        $datagrid = new Datagrid;
		$datagrid->setFields(array('sat_category' => Str::upper(Lang::line('admin.cataudit')->get())));
		$datagrid->setFields(array('sat_remark' => Str::upper(Lang::line('admin.auditmark')->get())));
        $datagrid->setFields(array('sup_nama' => Str::upper(Lang::line('admin.suid')->get())));
        $datagrid->setDate(array('sat_tarikh' => Str::upper(Lang::line('admin.tkhaudit')->get())));
        $datagrid->setTable('sys_audit_trail', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->build($allTrail, 'sat_id');

        return $datagrid->render();
	}
	
	/**
     * Function search date audit trail
	 *
	 * @param varchar $input the varchar to quote
	 *
	 * @return list search date.
     */
	public static function trailSearchDateList($input)
	{
        $operator = (stripos($input['searchdat'], '*'))? 'LIKE':'=';
        $val = str_replace("*", "", $input['searchdat']);
        $refval = (stripos($input['searchdat'], '*'))? '%'.$val.'%':$val;

        $allTrail = Audit::left_join('sys_user_profile', 'sys_audit_trail.su_id', '=', 'sys_user_profile.su_id')
                    ->where('sys_audit_trail.sat_tarikh', 'LIKE', '%'.$input['searchdat'].'%')
					->order_by('sat_tarikh', 'desc')
                    ->paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;
		$datagrid->setFields(array('sat_category' => Str::upper(Lang::line('admin.cataudit')->get())));
		$datagrid->setFields(array('sat_remark' => Str::upper(Lang::line('admin.auditmark')->get())));
        $datagrid->setFields(array('sup_nama' => Str::upper(Lang::line('admin.suid')->get())));
        $datagrid->setDate(array('sat_tarikh' => Str::upper(Lang::line('admin.tkhaudit')->get())));
        $datagrid->setTable('sys_audit_trail', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->build($allTrail, 'sat_id');

        return $datagrid->render();
	}
				
}

?>